package com.mcodesal.framework;

import javax.microedition.khronos.opengles.GL10;

import com.game.grid.R;
import com.game.grid.R.drawable;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;

public final class ResourceProvider {
   private static ResourceProvider instance = null;
   public enum Texture {
		PATH, WALL_ANIM, PLAYER_DOT, PLAYER_HEAD, PLAYER_BODY, PLAYER_TAIL, BACKGROUND_GRID,
		FIRE_PARTICLE, MENU_BACKGROUND, MENU_TITLE, STARTGAME, STARTGAME_PRESSED,
		OPTIONS, OPTIONS_PRESSED, HIGHSCORES, HIGHSCORES_PRESSED,
		HELP, HELP_PRESSED, CREDITS, CREDITS_PRESSED
   }
   private int[] textures = new int[20];
   private ResourceProvider() {
      // Exists only to defeat instantiation.
   }
   public static ResourceProvider getInstance() {
      if(instance == null) {
         instance = new ResourceProvider();
      }
      return instance;
   }
   
   public int getTexture(Texture textureEnum) {
		return textures[textureEnum.ordinal()];
   }
   public void loadGLTextures(GL10 gl, Context context) {
    	//Option for looping through all drawables instead of hard-coding
		/*
		Field[] drawables = android.R.drawable.class.getFields();
		for (Field f : drawables) {
			try {
				System.out.println("R.drawable." + f.getName());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		*/
		Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.path64);

		// loading texture
    	gl.glGenTextures(textures.length, textures, 0);
		// ...and bind it to our array
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.wall_anim);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[1]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.player_dot);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[2]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.glowworm_head);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[3]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.glowworm_body);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[4]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.glowworm_tail);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[5]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.nehe);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[6]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.fire_particle);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[7]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu_background);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[8]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu_title);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[9]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.startgame);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[10]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.startgame_pressed);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[11]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.options);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[12]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.options_pressed);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[13]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.highscores);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[14]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.highscores_pressed);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[15]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.help);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[16]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.help_pressed);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[17]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.credits);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[18]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.credits_pressed);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[19]);
		
		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
		
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		
		// create nearest filtered texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		
		// Clean up
		bitmap.recycle();
	}
}